package com.yiren.dbaa.module.practice_set.line.view.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.dbaa.R;

import org.apache.commons.lang.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/12/23
 */
public class BindLineAdapter extends BaseAdapter {

    private Activity activity;
    private MapEntity map;
    private List<Object> list;
    private OnLineSelectListener listener;

    private final static int TYPE_TITLE = 0;
    private final static int TYPE_LIST = 1;

    public BindLineAdapter(Activity activity, MapEntity map, List<Object> list, OnLineSelectListener listener) {
        this.activity = activity;
        this.map = map;
        this.list = list;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position) instanceof String) {
            return TYPE_TITLE;
        } else {
            return TYPE_LIST;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 holder1 = null;
        ViewHolder2 holder2 = null;
        int currentType = getItemViewType(position);
        if (convertView == null) {
            switch (currentType) {
                case TYPE_TITLE:
                    convertView = LayoutInflater.from(activity).inflate(R.layout.item_line_site_name, parent, false);
                    holder1 = new ViewHolder1(convertView);
                    convertView.setTag(holder1);
                    break;
                case TYPE_LIST:
                    convertView = LayoutInflater.from(activity).inflate(R.layout.item_line_name_select, parent, false);
                    holder2 = new ViewHolder2(convertView);
                    convertView.setTag(holder2);
                    break;
            }
        } else {
            switch (currentType) {
                case TYPE_TITLE:
                    holder1 = (ViewHolder1) convertView.getTag();
                    break;
                case TYPE_LIST:
                    holder2 = (ViewHolder2) convertView.getTag();
                    break;
            }
        }

        switch (currentType) {
            case TYPE_TITLE:
                holder1.tvSiteName.setText(list.get(position).toString());
                break;
            case TYPE_LIST:
                LineInfoEntity lineInfo = (LineInfoEntity) list.get(position);
                holder2.cbLineName.setText(lineInfo.getName());
                holder2.tvLineType.setText(lineInfo.getLineType() == 1 ? "考路路段" : "训练路段");
                if ((StringUtils.isNotEmpty(lineInfo.getMapId()) && (!lineInfo.getMapId().equals(map.getId()))) || (!map.getSiteCode().equals(lineInfo.getSiteCode()) && lineInfo.getLineType() == 1)) {
                    //如果线路的mapId不为空，并且不是本地图的id，则说明该线路已被其它地图绑定（或者非当前考场的考试路段）不可选择
                    holder2.cbLineName.setEnabled(false);
                } else {
                    holder2.cbLineName.setEnabled(true);
                    if (StringUtils.isNotEmpty(lineInfo.getMapId()) && lineInfo.getMapId().equals(map.getId())) {
                        holder2.cbLineName.setChecked(true);
                    }
                }
                holder2.cbLineName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        listener.onLineSelect(isChecked, position);
                    }
                });
                break;
        }
        return convertView;
    }

    static class ViewHolder1 {
        @BindView(R.id.tv_site_name)
        TextView tvSiteName;

        ViewHolder1(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder2 {
        @BindView(R.id.cb_line_name)
        CheckBox cbLineName;
        @BindView(R.id.tv_line_type)
        TextView tvLineType;

        ViewHolder2(View view) {
            ButterKnife.bind(this, view);
        }
    }


    public interface OnLineSelectListener {

        void onLineSelect(boolean selected, int position);
    }

}